// buy JavaScript Document
var buy = {
	homeInit : function() {
		datasInit();
		var isContent = "";
		$.each(buyDatas, function (n, value) {
			isContent += buy.content(value, n, true) + "#";
		});
		if(isContent.indexOf("0#") == -1) {
			$('#content').append("<div class=\"text-center\"><i>没有内容</i></div>");
		}
	},
	// 内容
	content : function(content, contentIndex, isJudge) {
		if(!content) return 1;
		var datas = content.datas;
		if(!datas || datas.length <= 0) return 2;
		var screenDatas = new Array();
		$.each(datas, function(n, value) {
			if(isJudge) {
				if(value.indexShow) {
					screenDatas.push(value);
				}	
			} else {
				screenDatas.push(value);
			}
		});
		if(!screenDatas || screenDatas.length <= 0) return 2;
		var contentHtml = "<div class=\"container\">";
		contentHtml += "<h3 class=\"animated wow zoomIn\">" +  content.title + "</h3>";
			if(isJudge)  contentHtml += "<p class=\"est animated wow zoomIn\"><a href='single.html?plate=" + contentIndex + "'>" + content.href_tips + "</a></p>";
				contentHtml += "<div class=\"new-collections-grids\">";
					contentHtml += "<div class=\"row\">";
					$.each(screenDatas, function(n, value) {
						var src = !value.images.src ? '' : value.images.src;
						var alt = !value.images.alt ? '' : value.images.alt;
						contentHtml += "<div class=\"col-sm-6 col-md-4\">";
							contentHtml += "<div class=\"thumbnail\">";
								contentHtml += "<img src='" + src + "' alt='" + alt + "'>";
								contentHtml += "<div class=\"caption\">";
									contentHtml += "<h4>" + value.intro + "</h4>";
									contentHtml += "<p>" + value.describe + "</p>";
									contentHtml += "<p><a href='" + value.taobao + "' class=\"btn btn-primary\" role=\"button\">商品链接</a> <a href='" + value.coupons + "' class=\"btn btn-default\" role=\"button\">领券链接</a></p>";
								contentHtml += "</div>";
							contentHtml += "</div>";
						contentHtml += "</div>";
						
					});
              		contentHtml += "</div>";
              	contentHtml += "<div class=\"clearfix\"></div>";
              contentHtml += "</div>";
        contentHtml += "</div>";
		$('#content').append(contentHtml);
		return 0;
	},
	
	// 查询
	onSearch : function(isJudge) {
		var val = $('#the_key').val();
		var isContent = "";
		$('#content').empty();
		$.each(buyDatas, function (i, value) {
			// -- content
			if(!val) {
				isContent += buy.content(value, i, isJudge) + "#";
			} else {
				var datas = value.datas;
				var judgeData = new Array();
				if(datas && datas.length > 0){
					$.each(datas, function(j, data) {
						if(data.indexShow == isJudge) {
							if(data.intro.indexOf(val) != -1 || data.describe.indexOf(val) != -1) {
								judgeData.push(data);
							}	
						}
					});
					if(judgeData && judgeData.length > 0) {
						value.datas = judgeData;
						isContent += buy.content(value, i, false) + "#";
					}
				}
			}
			// -- end content
		});
		if(isContent.indexOf("0#") == -1) {
			$('#content').append("<div class=\"text-center\"><i>该板块没有内容</i></div>");
		}
		datasInit();
	},
	
	// 板块详细
	singleInit : function(contentIndex, searchValue) {
		$('#content').empty();
		$('#sd-plate-hidden').val(contentIndex);
		datasInit();
		if (contentIndex == null ||  contentIndex < 0 || isNaN(contentIndex)) {
			$('#content').append("<div class=\"text-center\"><i>该板块数据不正确!</i></div>");
		} else if (buyDatas.length <= 0) {
			$('#content').append("<div class=\"text-center\"><i>该板块没有内容!</i></div>");
		} else if(buyDatas[contentIndex].length <= 0) {
			$('#content').append("<div class=\"text-center\"><i>该板块没有内容!</i></div>");
		} else {
			var maxResult = 11;
			var platePages = buy.pageRows(contentIndex, searchValue, 1, maxResult);
			console.log(platePages);
			var isContent = buy.content(platePages, contentIndex, false);
			if(isContent != 0) {
				$('#content').append("<div class=\"text-center\"><i>该板块没有内容</i></div>");
			} else {
				// 分页栏
				createPageNav({
					$container:$("#single_page"),
					className: "pagination pagination-small",
					pageCount: parseInt((platePages.pageSize + maxResult - 1) / maxResult),
					maxCommonLen: 5,
					afterFun: function(v1) {
						$('#content').empty();
						var platePages = buy.pageRows(contentIndex, searchValue, v1, maxResult);
						var isContent = buy.content(platePages, contentIndex, false);
						if(isContent != 0) {
							$('#content').empty();
							$('#content').append("<div class=\"text-center\"><i>该分页没有内容</i></div>");
						}
					}
				});
			}
			return true;
		}
	},
	
	// 分页处理
	pageRows : function(platesIndex, searchValue, page, maxResult) {
		var plates = buyDatas[platesIndex];
		var datas = plates.datas;
		var searchData = new Array();
		if(searchValue) {
			for(var i = 0; i < datas.length; i++) {
				if(datas[i].intro.indexOf(searchValue) != -1 || datas[i].describe.indexOf(searchValue) != -1) {
					searchData.push(datas[i]);
				}
			}
			datas = searchData;
			plates.pageSize = datas.length;
		}
		var pageEnd = page * maxResult - 1;
		var pageIndex = pageEnd - maxResult + 1;
		if((page - 1) == 0) {
			pageIndex = 0;
		}
		if(datas.length > pageEnd) {
			var pageDatas = [];
			for(var i = pageIndex; i <= pageEnd; i ++) {
				pageDatas.push(datas[i]);
			}
			plates.datas = pageDatas;
		} else {
			var pageDatas = [];
			for(var i = pageIndex; i < datas.length; i ++) {
				pageDatas.push(datas[i]);
			}
			plates.datas = pageDatas;
		}
		datasInit();
		if(!searchValue) {
			plates.pageSize = buyDatas[platesIndex].datas.length;
		}
		return plates;
	},
	
	// 板块查询
	singleSearch : function () {
		var platesIndex = $('#sd-plate-hidden').val();
		if(!platesIndex) {
			platesIndex = -1;
		}
		
		var searchValue = $('#sd-search-hidden').val();
		if(!searchValue) {
			searchValue = null;
		}
		buy.singleInit(platesIndex, searchValue);
	}
}